/**
 * Created by saturn on 2018/3/6.
 */

// 日期格式化
function formatTime(number) {
  var time = new Date(Number(number));
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var h = time.getHours();
  var mm = time.getMinutes();
  var s = time.getSeconds();
  return y + '-' + zero(m) + '-' + zero(d) + ' ' + zero(h) + ':' + zero(mm) + ':' + zero(s);
}

// 补零
function zero(m) {
  return m < 10 ? '0' + m : m
}

// 反序列化
function url2json(url) {
  if (url.indexOf("?") > -1) {
    url = url.substring(1);
  }
  var parts = url.split("&");
  var json = {};
  parts.forEach(function (item) {
    if (item.indexOf("=") > -1) {
      var itemArray = item.split("=");
      json[itemArray[0]] = itemArray[1];
    }
  });
  return json;
}

/**
 * 根据formId将from里的所有元素转成对象
 * @param form
 */
var Commons = {
  formData: function (selector, params, withoutTemp) {
    var formValues = $(selector).serializeArray();
    var paramObject = {};
    if (params) {
      paramObject = params;
    }
    for (var i = 0, len = formValues.length; i < len; i++) {
      if (!withoutTemp || !formValues[i].name.startsWith("temp_"))
        if (formValues[i].value === '' || formValues[i].value === null) {
          paramObject[formValues[i].name] = undefined;
        } else {
          paramObject[formValues[i].name] = Commons.xssFilter(formValues[i].value);
        }
    }
    return paramObject;
  },
  /**
   * 获取选中的id
   * @param table
   * @param row
   * @returns {*}
   */
  getIdSelections: function (table, row) {
    return $.map(table.bootstrapTable('getSelections'), row);
  },

  /**
   * Js端简单Xss过滤
   * @param s
   * @returns {string}
   */
  xssFilter: function (s) {
    return s.replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        //.replace(/\//g, '&#x2f;')
        .replace(/"\s*[a-zA-Z0-9]*\s*javascript:/g, '&quot;javascript:')
        .replace(/'\s*[a-zA-Z0-9]*\s*javascript:/g, '&#x27;javascript:');
  },
  onLoadError: function (status) {
    Messager.warn("加载数据失败 Status:" + status);
  },
  append0Left: function (str, length) {
    if (str.length >= length)
      return str;
    else
      return Commons.append0Left("0" + str, length);
  }
};